package com.guch.help.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class GridProgressBar extends View {

    private Paint paint;
    private int columns;
    private int progress;

    public GridProgressBar(Context context) {
        super(context);
        init();
    }

    public GridProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.FILL);
        columns = 10; // 假设我们有10列方格
        progress = 5; // 假设进度是5列
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        int cellWidth = width / columns;

        for (int i = 0; i < columns; i++) {
            if (i < progress) {
                // 画一个填充的方格
                canvas.drawRect(i * cellWidth, 0, (i + 1) * cellWidth+10, height, paint);
            } else {
                // 画一个空的方格
                paint.setStyle(Paint.Style.STROKE);
                canvas.drawRect(i * cellWidth, 0, (i + 1) * cellWidth + 10, height, paint);
                paint.setStyle(Paint.Style.FILL);
            }
        }
    }

    // 设置进度
    public void setProgress(int progress) {
        this.progress = progress;
        invalidate(); // 重绘View
    }
}